pub mod dao;
pub mod entity;
mod mutation;
#[allow(unused)]
mod password;
mod util;
pub use mutation::Mutation;

use sea_orm::ColumnTrait;
use sea_orm::EntityTrait;
use sea_orm::QueryFilter;
use sea_orm::QueryOrder;
use sea_orm::QuerySelect;

/// 获得 casbin policies.
/// # return
/// role_id, path, method, eft
/// # params
/// - role_id 可选，过滤对应的role
/// - resource_id 可选，过滤对应的resource
pub async fn casbin_policies(
    db: &sea_orm::DbConn,
    role_id: Option<i32>,
    resource_id: Option<i32>,
) -> crate::dao::Result<Vec<(i32, String, String, bool)>> {
    let mut sql = entity::sys_role::Entity::find()
        .select_only()
        .column(entity::sys_role::Column::Id)
        .column(entity::sys_resource::Column::Path)
        .column(entity::sys_role_resource::Column::Method)
        .column(entity::sys_role_resource::Column::Allow)
        .inner_join(entity::sys_resource::Entity)
        .filter(entity::sys_resource::Column::Status.eq(true))
        .filter(entity::sys_role::Column::Status.eq(true))
        .order_by_asc(entity::sys_role::Column::Id)
        .order_by_asc(entity::sys_resource::Column::Path);
    if let Some(id) = role_id {
        sql = sql.filter(entity::sys_role::Column::Id.eq(id));
    }
    if let Some(id) = resource_id {
        sql = sql.filter(entity::sys_resource::Column::Id.eq(id));
    }
    sql.into_tuple().all(db).await
}
